package com.tqfframe.user.controller;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.tqfframe.user.dao.OrderMapper;
import com.tqfframe.user.domain.SysUser;
import com.tqfframe.user.entry.OrderEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by Tang-QiFeng on 2020/6/24
 */
@RestController
public class TestController {

    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private OrderMapper orderMapper;

    /**
     *  本地
     *  http://localhost:8080/api/user/userClick/11111
     *  http://localhost:9000/userClick/11111
     *
     *  docker
     *  http://192.168.47.135:8080/api/user/userClick/11111
     *  http://192.168.47.135:9000/userClick/11111
     *
     *     模拟用户点击+1
     *
     * @param userid
     * @return
     */
    @GetMapping(value = "/userClick/{userid}")
    public String userClick(@PathVariable(name = "userid") String userid){
        System.out.println(userid);
        logger.info("test kafka info");
        logger.info("我是用户，我通过其他微服务进行点击+1了");
        logger.error("test kafka error");
        logger.debug ("test kafka debug");
//        logger.info("我是用户，我通过其他微服务进行点击+1了");
//        logger.error("我是错误日志，我会被打印");
//        logger.debug ("我是DEBUG日志，我不会被打印，因为最低打印级别为INFO");
        return userid;
    }

    /**
     *  本地
     *  http://localhost:9000/updateOrder/1
     *
     * @return
     */
    @Transactional
    @LcnTransaction
    @GetMapping(value = "/updateOrder/{state}")
    public String updateOrder(@PathVariable(name = "state") String state){
        OrderEntity o=new OrderEntity();
        o.setId(1);
        o.setState(Integer.valueOf(state));
        int i= orderMapper.updateById(o);
        if(i==0){
            return "fail";
        }else{
            logger.debug("在这里模拟卡顿，查看事务是否成功，事实证明，主微服务没有插入数据，当前微服务没有修改数据！");
//            try {
//                Thread.sleep(20000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            return "ok";
        }
    }

    /**
     *  本地
     *  http://localhost:8080/api/user/userClickTimeOut/11111
     *  http://localhost:9000/userClickTimeOut/11111
     *
     *  docker
     *  http://192.168.47.135:8080/api/user/userClickTimeOut/11111
     *  http://192.168.47.135:9000/userClickTimeOut/11111
     *
     *     模拟用户点击+1
     *
     * @param userid
     * @return
     */
    @GetMapping(value = "/userClickTimeOut/{userid}")
    public String userClickTimeOut(@PathVariable(name = "userid") String userid){
        logger.info("故意设置5000秒进行熔断降级操作，相当于这个方法执行了超过5秒，默认的feign调用时间不超过1秒。不然就直接进熔断了！！！");
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("我是用户，我通过其他微服务进行点击+1了");
        return "我是B服务，我被成功调用了！！！";
    }


    @RequestMapping(value = "/testPost",method = RequestMethod.POST)
    public String issueByIds(@RequestBody SysUser sysUser){

        return sysUser.getUsername();
    }
}
